import BaseResult from '../lib/BaseResult.js'
import axios from "axios";
// var http = require('http');
class Sign {
    static async set(ctx) {
        let data = ctx.query;
        let host = ctx.request.header.host;
        let url = 'http://127.0.0.1:3000/server/sign/get?token=' + data.token;
        if (host.indexOf('www.lsdfly.com') > -1) {
            url = 'https://www.lsdfly.com/server/account/get?token=' + data.token
        }
        console.log('请求：', url)
        return new Promise((resolve) => {
                axios.get(url)
                    .then(res => {
                        console.log(res.data, '设置session')
                        if (res.data.code == 0) {
                            console.log(data.token, 'data.token', res.data.data)
                            ctx.cookies.set("token", data.token);
                            ctx.session[data.token] = res.data.data;
                            console.log('读取的:', ctx.session[data.token])
                        }
                        ctx.body = '';
                        resolve();
                    })
            })
            // ctx.status=404;

        // console.log(ctx.request.header.host)
        // let sign =  new Buffer(data.sign,'base64');
        //ctx.session[data.token] = JSON.parse(sign.toString());
        // ctx.body =`
        // <script>gg
        // localStorage.setItem('token','${data.token}');
        // location.replace('/404.html');
        // </script>       
        // `;
    }
    static async get(ctx, next) {
        //   let token = ctx.request.body.token||ctx.query.token||ctx.request.header.token;
        console.log(ctx.token, 'ctx.token', ctx.session[ctx.token])
        console.log(ctx.session)
        if (!ctx.token || !ctx.session[ctx.token]) {
            ctx.body = new BaseResult(9999, null, null);
            return false;
        }
        ctx.body = new BaseResult(0, null, ctx.session[ctx.token]);
    }
}
export default Sign;